package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.AdminExample;
import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.entity.RoleExample;
import com.atguigu.crowd.mapper.RoleMapper;
import com.atguigu.crowd.service.api.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zzqstart
 * @Description
 * @date 2022-09-03 20:52
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Role> getRolePage(String keyword, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<Role> list = roleMapper.selectRoleByKeyword(keyword);

        PageInfo<Role> rolePageInfo = new PageInfo<>(list);

        return rolePageInfo;
    }

    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void editRole(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public void remove(List<Integer> list) {

        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(list);

        roleMapper.deleteByExample(roleExample);
    }

    @Override
    public List<Role> getAssignedRole(Integer adminId) {
        List<Role> role = roleMapper.selectAssignedRole(adminId);
        return role;
    }

    @Override
    public List<Role> getUnAssignedRole(Integer adminId) {
        List<Role> role = roleMapper.selectUnAssignedRole(adminId);
        return role;
    }

    @Override
    public void saveAdminRoleRelationship(Integer adminId, List<Integer> roleIdList) {
        roleMapper.deleteAdminRoleByAdminId(adminId);

        if (roleIdList == null || roleIdList.size() == 0) {
            return;
        }

        roleMapper.insertAdminRoleRelationship(adminId, roleIdList);
    }
}
